import { Form, Input, Button, message, Typography } from 'antd';
import { UserOutlined, LockOutlined } from '@ant-design/icons';
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { accountService } from '@/services/accountService';
import styles from './Login.module.css';

const { Title } = Typography;

interface LoginForm {
  username: string;
  password: string;
}

const Login: React.FC = () => {
  const navigate = useNavigate();
  const [loading, setLoading] = useState(false);

  const handleSubmit = async (values: LoginForm) => {
    try {
      setLoading(true);
      const response = await accountService.login(values);
      if (response.data.token) {
        message.success('登录成功！');
        navigate('/');
      }
    } catch (error: any) {
      if (error.response?.data?.non_field_errors) {
        message.error(error.response.data.non_field_errors[0]);
      } else {
        message.error('登录失败，请重试！');
      }
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className={styles.loginContainer}>
      <div className={styles.decorationBackground}>
        <div className={styles.blob1}></div>
        <div className={styles.blob2}></div>
        <div className={styles.blob3}></div>
        <div className={styles.dot1}></div>
        <div className={styles.dot2}></div>
      </div>
      
      <div className={styles.loginBox}>
        <div className={styles.formBox}>
          <div className={styles.titleWrapper}>
            <Title level={2} className={styles.title}>欢迎回来</Title>
            <p className={styles.subtitle}>登录您的账户，开始创作之旅</p>
          </div>
          
          <Form
            name="login"
            onFinish={handleSubmit}
            size="large"
          >
            <Form.Item
              name="username"
              rules={[
                { required: true, message: '请输入用户名！' },
                { min: 3, message: '用户名至少3个字符!' }
              ]}
            >
              <Input
                prefix={<UserOutlined />}
                placeholder="用户名"
                autoComplete="username"
              />
            </Form.Item>

            <Form.Item
              name="password"
              rules={[
                { required: true, message: '请输入密码！' },
                { min: 6, message: '密码至少6个字符!' }
              ]}
            >
              <Input.Password
                prefix={<LockOutlined />}
                placeholder="密码"
                autoComplete="current-password"
              />
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                className={styles.submitBtn}
                loading={loading}
                block
              >
                登录
              </Button>
            </Form.Item>

            <div className={styles.bottomText}>
              <p>
                还没有账号？
                <Button 
                  type="link" 
                  onClick={() => navigate('/register')} 
                  className={styles.toggleBtn}
                >
                  立即注册
                </Button>
              </p>
              <span className={styles.forgotPassword}>忘记密码？</span>
            </div>
          </Form>
        </div>
      </div>
    </div>
  );
};

export default Login;
